<?php

require('../database/bd_connect.php');
 
if (isset($_GET['opt'])) 
{
 $cpe = $_GET['opt'] ;

   echo $time;

   $sql = "SELECT cpe.city AS City, count(distinct if(wifi_data.ssid LIKE 'VODA%', wifi_data.ssid, NULL)) AS Vodafone, count(distinct if(wifi_data.ssid LIKE 'MEO%', wifi_data.ssid, NULL)) AS MEO, count(distinct if(wifi_data.ssid LIKE 'ZON%', wifi_data.ssid, NULL)) AS ZON, (count(distinct wifi_data.ssid)-count(distinct if(wifi_data.ssid LIKE 'VODA%', wifi_data.ssid, NULL))-count(distinct if(wifi_data.ssid LIKE 'MEO%', wifi_data.ssid, NULL))- count(distinct if(wifi_data.ssid LIKE 'ZON%', wifi_data.ssid, NULL))) AS Others FROM wifi_data, cpe WHERE wifi_data.cpe_id = cpe.cpe_id and date_format(wifi_data.time ,'%Y-%m-%d') = " . $time . " GROUP BY cpe.city;";

 $result = $conn->query($sql);

 $regions = array();
 while($row = mysqli_fetch_array($result))
 {
  $city = $row['City'];
  $time = $row['Time'];
  $voda = $row['Vodafone'];
  $meo = $row['MEO'];
  $zon = $row['ZON'];
  $oth = $row['Others'];

  if ( ! array_key_exists($city, $regions))
  {
   $regions[$city] = array();
  }

  $data = array();

  array_push($data, $time);
  array_push($data, $voda);
  array_push($data, $meo);
  array_push($data, $zon);
  array_push($data, $oth);

//  echo json_encode($data);

  array_push($regions[$city],$data);
 }
 
 echo json_encode($regions);
  
} 
else 
{ 
 echo "erro" ;
}
mysqli_close($conn);
?>
